package com.tanhua.manage.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.manage.mapper.LogMapper;
import com.tanhua.manage.pojo.Log;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LogService_six {

    @Autowired
    private LogMapper logMapper;


    /**
     * 根据Id降序查询登录操作 索引0为最近操作,集合长度-1索引则为第一次操作(注册时间)
     *
     * @param id
     * @return
     */
    public List<Log> queryLogById(Long id) {

        QueryWrapper<Log> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("user_id", id)
                .eq("type", "01")
                .orderByDesc("log_time");


        return this.logMapper.selectList(queryWrapper);
    }


    public List<Log> queryLogList(List<Long> ids) {

        List<Log> logList = new ArrayList<>();

        for (Long id : ids) {
            List<Log> logs = this.queryLogById(id);
            if (CollectionUtils.isEmpty(logs)) {
                Log log = new Log();
                log.setUserId(id);
                log.setLogTime("2012-12-01");
                log.setPlace("上海市");
                logList.add(log);
            }

            if (CollectionUtils.isNotEmpty(logs)) {
                logList.add(logs.get(0));
            }

        }
        return logList;
    }
}
